package com.weiaibenpao.demo.chislim.service;

import android.app.job.JobParameters;
import android.app.job.JobService;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.widget.Toast;

import com.weiaibenpao.demo.chislim.BuildConfig;

/**
 * Created by ZJL on 2017/11/10.
 *  用户点击开始跑步才启动保活，点击结束跑步结束保活
 */

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class KeepAliveService extends JobService {
    private static final String TAG = "KeepAliveService";
    public static final int JOB_KEEP_ALIVE = 119;

    @Override
    public boolean onStartJob(JobParameters params) {
        Log.i(TAG,"onStartJob");
        startRunningService();
        jobFinished(params,false);
        return false;
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        Log.i(TAG,"onStopJob");
        startRunningService();
        return false;
    }


    @Override
    public void onTaskRemoved(Intent rootIntent) {
        Log.i(TAG,"onTaskRemoved");
        startRunningService();
    }

    public void startRunningService(){
        startService(RunningService.getKeepAliveIntent(this));
        if(BuildConfig.DEBUG){
            Toast.makeText(this,"KeepAlive",Toast.LENGTH_SHORT).show();
        }
    }

}
